/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hro.mobiqueen.ui;

import java.io.ByteArrayInputStream;
import java.util.List;

import nl.hro.mobiqueen.R;
import nl.hro.mobiqueen.data.DatabaseAdapter;
import nl.hro.mobiqueen.data.objects.Podium;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * 
 * @author jv
 */
public class Plattegrond extends MobiBaseMaps {

    private MapView mMapView;
    private List<Overlay> mMapOverlays;
    private MyLocationOverlay mMyLocation;
    

    //jv: Moet nog worden goedgezet, staat nu op Amsterdam CS
    private double startLocationLat = 52.372845;
    private double startLocationLon = 4.893658;
        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plattegrond);

        initializeMap();
        initializeMapItems();
        initializeMyLocation();
    }

    private void initializeMap() {
        mMapView = (MapView) findViewById(R.id.mapview);
        mMapView.setBuiltInZoomControls(true);
        mMapView.getController().setZoom(15);
        mMapOverlays = mMapView.getOverlays();
    }



    private void initializeMapItems() {
        DatabaseAdapter databaseAdapter = new DatabaseAdapter(this).open();

        Cursor podiaCursor = databaseAdapter.listPodia();
        podiaCursor.moveToFirst();
        while (!podiaCursor.isAfterLast()) {
            Podium podium = DatabaseAdapter.getPodiumFromCursor(podiaCursor);
            Double lat = podium.getLatitude() * 1E6;
            Double lon = podium.getLongitude() * 1E6;
            
			Bitmap icon = BitmapFactory.decodeStream(new ByteArrayInputStream(
					podium.getMapsIcon()));
			Matrix matrix = new Matrix();
			int width = icon.getWidth();
			int height = icon.getHeight();
			int newWidth = 25;
			int newHeight = 25;
			float scaleWidth = ((float) newWidth) / width;
			float scaleHeight = ((float) newHeight) / height;

			matrix.postScale(scaleWidth, scaleHeight);
			Bitmap resizedIcon = Bitmap.createBitmap(icon, 0, 0, width, height,
					matrix, true);

            addMapOverlayItem(new BitmapDrawable(resizedIcon), new GeoPoint(lat.intValue(), lon.intValue()), podium.getName(), podium.getDescription());

            podiaCursor.moveToNext();
        }
    }
    
    public void addMapOverlayItem(Drawable drawable, GeoPoint point,
        String text1, String text2) { //getApplicationContext(getApplicationContext)

        GoogleRoadParameters params = new GoogleRoadParameters();
        params.setStartLocationLat(startLocationLat);
        params.setStartLocationLon(startLocationLon);
        params.setMapview(mMapView);

        GoogleMapItems itemizedoverlay = new GoogleMapItems(drawable, this, params);
        OverlayItem overlayitem = new OverlayItem(point, text1, text2);
        itemizedoverlay.addOverlay(overlayitem);
        mMapOverlays.add(itemizedoverlay);
    }

    private void initializeMyLocation() {
        mMyLocation = new MyLocationOverlay(this, mMapView);
        mMapOverlays.add(mMyLocation);
    }

    @Override
    public void onResume() {
        super.onResume();
        //mMyLocation.enableMyLocation();
        mMyLocation.enableCompass();
    }

    @Override
    public void onPause() {
        super.onPause();
        //mMyLocation.disableMyLocation();
        mMyLocation.disableCompass();
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}
